const nodemailer = require('nodemailer');

// 创建邮件发送器
const transporter = nodemailer.createTransport({
  service: 'qq',
  port: 465,
  secure: true,
  auth: {
    user: '486571909@qq.com',
    pass: 'ydzfqajngknncahb'
  }
});

// 生成6位数字验证码
const generateVerificationCode = () => Math.floor(100000 + Math.random() * 900000).toString();

/**
 * 发送验证码邮件
 */
const sendVerificationEmail = async (email, code) => {
  try {
    const mailOptions = {
      from: '486571909@qq.com',
      to: email,
      subject: 'Zeno 电商系统 - 邮箱验证码',
      html: `<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; padding: 20px; border: 1px solid #e0e0e0; border-radius: 5px;"><h2 style="color: #333; text-align: center;">Zeno 电商系统</h2><p style="font-size: 16px; color: #555;">您好，</p><p style="font-size: 16px; color: #555;">您正在注册 Zeno 电商系统账号。您的验证码是：</p><div style="background-color: #f7f7f7; padding: 15px; text-align: center; margin: 20px 0; border-radius: 4px;"><span style="font-size: 24px; font-weight: bold; letter-spacing: 5px;">${code}</span></div><p style="font-size: 14px; color: #777;">该验证码将在 10 分钟后失效。如果不是您本人操作，请忽略此邮件。</p><div style="margin-top: 30px; padding-top: 15px; border-top: 1px solid #e0e0e0; text-align: center; font-size: 12px; color: #999;"><p>© ${new Date().getFullYear()} Zeno 电商系统 - 此邮件由系统自动发送，请勿回复</p></div></div>`
    };
    // 发送邮件
    const info = await transporter.sendMail(mailOptions);
    return { success: true, messageId: info.messageId };
  } catch (error) {
    // 发送失败
    return { success: false, error: error.message };
  }
};

module.exports = { generateVerificationCode, sendVerificationEmail }; 